package swagger.boot.starter.autoconfigure;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;

import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
import swagger.boot.starter.autoconfigure.properties.DocketInfo;
import swagger.boot.starter.autoconfigure.properties.SwaggerProperties;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

@EnableSwagger2WebMvc
@EnableConfigurationProperties({SwaggerProperties.class})
@Configuration
public class SwaggerAutoConfiguration implements BeanFactoryAware {
    @Autowired
    private SwaggerProperties swaggerProperties;

    private BeanFactory beanFactory;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

    @Bean
    @ConditionalOnMissingBean
    public List<Docket> createDocket() {
        List<Docket> docketList = new ArrayList<>();
        ConfigurableBeanFactory configurableBeanFactory=(ConfigurableBeanFactory) beanFactory;
        // 分组创建
        for (String groupName : swaggerProperties.getDocketGroup().keySet()) {
            DocketInfo docketInfo =
                    swaggerProperties.getDocketGroup().get(groupName);
            ApiInfo apiInfo = new ApiInfoBuilder()
                    //页面标题
                    .title(docketInfo.getTitle())
                    //创建人
                    .contact(new Contact(docketInfo.getContactInfo().getName(),
                            docketInfo.getContactInfo().getUrl(),
                            docketInfo.getContactInfo().getEmail()))

                    //版本号
                    .version(docketInfo.getVersion())
                    //描述
                    .description(docketInfo.getDescription())
                    .build();


            Docket docket = new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo)
                    .groupName(docketInfo.getGroup())
                    .select()
                    //为当前包路径
                    .apis(RequestHandlerSelectors.basePackage(docketInfo.getBasePackage()))
                    .paths(PathSelectors.any())
                    .build();
            configurableBeanFactory.registerSingleton(groupName, docket);
            docketList.add(docket);
        }
        return docketList;
    }


}


